<template>
  <div class="home">
   <el-container>
     <!-- 侧边栏 -->
  <el-aside width="auto">
    <div class="logo"></div>
     <el-menu
      default-active="2"
      unique-opened
      router
      :collapse="iscollapse"
      class="el-menu-admin"
      background-color="#F9F9F9"
      @open="handleOpen"
      @close="handleClose">
      <el-submenu :index="menu.path" v-for="menu in menusList" :key="menu.id">
        <template slot="title">
          <i class="el-icon-tickets"></i>
          <span>{{menu.authName}}</span>
        </template>
         <el-menu-item :index="menuData.path" v-for="menuData in menu.children" :key="menuData.id">
              <i class="el-icon-menu"></i>
              <span>{{menuData.authName}}</span>
         </el-menu-item>
     </el-submenu>
    </el-menu>
  </el-aside>
  <el-container>
    <!-- 头部 -->
    <el-header>
        <div class="toggle-btn" @click="toggle">
        <i class="myicon myicon-menu"></i>
      </div>
      <div class="system-title">电商后台管理系统 </div>
      <div>
        <span class="welcome">您好,{{username}}</span>
          <el-button type="warning" round @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-main>
      <transition
        enter-active-class="animated fadeIn"
        leave-active-class="animated fadeOut"
        mode="out-in">
        <router-view></router-view>
      </transition>
    </el-main>
  </el-container>
</el-container>
  </div>
</template>

<script>
import { getMenus } from '@/api'
export default {
  created () {
    this.username = localStorage.getItem('username')
    this.initMenus()
  },
  data () {
    return {
      iscollapse: false,
      username: '',
      menusList: []
    }
  },
  methods: {
    initMenus () {
      getMenus()
        .then(res => {
          // console.log(res)
          this.menusList = res.data.data
        })
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    toggle () {
      this.iscollapse = !this.iscollapse
    },
    logout () {
      localStorage.clear()
      this.$router.push({ name: 'login' })
    }

  }
}
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
}
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
  }
   .toggle-btn:hover {
      background-color: #4292CF;
    }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome{
    color: white;
  }
</style>
